package user

import (
	"github.com/asdine/storm/v3"
	"sync"
)

type Operate struct {
	db   *storm.DB
	lock sync.Mutex
}

func NewUserData(db *storm.DB) *Operate {
	return &Operate{db: db}
}

func (o *Operate) FindUserByName(username string) (*User, error) {
	var info User
	err := o.db.One("Username", username, &info)
	if err != nil {
		if err == storm.ErrNotFound {
			return nil, nil
		}
		return nil, err
	}
	return &info, nil
}

func (o *Operate) Update(user User) error {
	return o.db.Update(&user)
}

func (o *Operate) FindById(id int)(*User, error) {
	var info User
	err := o.db.One("ID", id, &info)
	if err != nil {
		if err == storm.ErrNotFound {
			return nil, nil
		}
		return nil, err
	}
	return &info, nil
}